/*
  菜单管理
*/
const { model, Sequelize } = require('../../core/model.js');

const menusModel = model.define('menus', {
	menu_name: {
		type: Sequelize.STRING(100),
		allowNull: false,
		validate: {
			is: {
				args: [/^.{1,18}$/],
				msg: '必须1-18个字符'
			}
		},
	},
	parent_id: {
		allowNull: false,
		type: Sequelize.INTEGER,
		defaultValue: 0,
		validate: {
			isExist: async (value) => {
				if (value == 0) return value;
				if (!await menusModel.findOne({ where: { id: value }, raw: true })) {
					throw new Error('parent_id不存在')
				}
			},
		}
	},
	router: {//前端路由
		type: Sequelize.STRING(100),
		validate: {
			is: {
				args: [/^(\/\w{0,})+$/],
				msg: '路由格式错误',
			}
		},
	},
	icon:{
		type: Sequelize.STRING(100),
	},
	status: {//是否显示
		type: Sequelize.ENUM('1', '0'), //1:显示，0:不显示
		defaultValue: '1',
		validate: {
			isIn: {
				args: [['1', '0']],
				msg: 'status只能取值1（显示）或0(不显示)'
			}
		}
	},
	sort: Sequelize.DATE,
	ctime: Sequelize.DATE,
	mtime: Sequelize.DATE,
}, {
		createdAt: 'ctime',
		updatedAt: 'mtime',
	})

menusModel.sync({
	//  force: true
})

module.exports = {
	menusModel,
	model,
	Op: Sequelize.Op
};